package com.xcw.bos.web.action;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.xcw.bos.domain.User;
import com.xcw.bos.utils.BosContext;
import com.xcw.bos.utils.MD5Utils;
import com.xcw.bos.web.action.base.BaseAction;

@Controller
@Scope("prototype")//原型，当访问Action是创建对象
public class UserAction extends BaseAction<User>{

	public UserAction(){
		System.out.println("UserAction创建了。。。");
	}
	
	//属性驱动，接收验证码
	private String checkcode;
	
	//用户登录方法--使用shiro提供的方式进行认证
	public String login(){
		Subject subject = SecurityUtils.getSubject();//当前用户，状态为未认证状态
		String password = MD5Utils.md5(model.getPassword());//使用md5加密
		AuthenticationToken token = new UsernamePasswordToken(model.getUsername(), password);
		try {
			subject.login(token);
			User user = (User)subject.getPrincipals();
			ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
		} catch (UnknownAccountException e) {
			e.printStackTrace();
			this.addActionError("账户不存在！");
			return "login";
		}catch (IncorrectCredentialsException e){
			e.printStackTrace();
			this.addActionError("密码错误！");
			return "login";
		}
		return "home";
	}
	//用户登录方法
	public String login_bak(){
		//从session中获取自动生成的验证码
		String validateCode = (String) ServletActionContext.getRequest()
				.getSession().getAttribute("key");
		
		if(StringUtils.isBlank(checkcode) || !checkcode.equals(validateCode)){
			//验证码错误
			//设置错误提示信息
			this.addActionError(this.getText("checkcodeerror"));
			//跳转到登陆页面
			return "login";
		}else{
			//验证码正确
			User user = userService.login(model);
			if(user != null){
				//登录成功，跳转到系统首页，将当前用户放入session
				ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
				return "home";
			}else{
				//登录失败，跳转到登陆页面，显示错误提示信息
				this.addActionError(this.getText("loginFail"));
				return "login";
			}
		}
	}
	
	//用户退出
	public String logout(){
		ServletActionContext.getRequest().getSession().invalidate();
		return "login";
	}
	
	//修改密码
	public String editPassword() throws IOException{
		String f = "1";
		try {
			userService.editPassword(model.getPassword(), BosContext.getLoginUser().getId());
		} catch (Exception e) {
			f = "0";
		}
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(f);
		return NONE;
	}
	
	private String[] roleIds;
	public void setRoleIds(String[] roleIds){
		this.roleIds = roleIds;
	}
	
	//保存用户
	public String save(){
		userService.save(model, roleIds);
		return "list";
	}
	
	//查询所有用户
	public String list(){
		List<User> list = userService.findAll();
		String[] excudes = new String[]{"noticebills","roles"};
		this.writeList2Json(excudes, list);
		return NONE;
	}
	
	public void setCheckcode(String checkcode){
		this.checkcode = checkcode;
	}
}
